package com.chixing.service.impl;

import com.chixing.mapper.HotelMapper;
import com.chixing.pojo.Hotel;
import com.chixing.pojo.HotelExample;
import com.chixing.pojo.HousewonExample;
import com.chixing.service.HotelService;
import com.chixing.service.HouseaddService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author dwe
 * @date 2021-10-21 20:20
 */
@Service
public class HouseaddServiceImpl implements HouseaddService {
    @Autowired
    private HotelMapper hotelMapper;
    @Override
    public ServerResponse saveHouse(Hotel hotel) {
        int rows = hotelMapper.insert(hotel);
        if (rows > 0 ){
            return ServerResponse.saveSuccess(rows);
        }
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse getByHouseIdAndHousewonName(String hotelName) {
        Hotel hotel = null;
        HotelExample example = new HotelExample();
        example.createCriteria().andHotelNameEqualTo(hotelName);
        List<Hotel> hotelList= hotelMapper.selectByExample(example);
        if (hotelList.size()>0){
            hotel = hotelList.get(0);
            return ServerResponse.getSuccess(hotel);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByHousewonNameAndPage(Integer housewonId, Integer pageNum) {
        HotelExample hotelExample = new HotelExample();
        hotelExample.createCriteria().andHousewonIdEqualTo(housewonId);
        PageHelper.startPage(pageNum,5);
        List<Hotel> hotelList = hotelMapper.selectByExample(hotelExample);
        PageInfo<Hotel> pageInfo = new PageInfo<>(hotelList);

        if (hotelList !=null &&hotelList.size()>0){
            return ServerResponse.getSuccess(pageInfo);
        }
        return ServerResponse.getFail(pageInfo);
    }


}
